<?php

namespace App\Admin\Controllers;

use App\Admin\Exports\WithdrawExport;
use App\Admin\Grid\NGrid;
use App\Models\WithdrawModel;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class WithdrawController extends BaseController
{
    protected $title = '提现记录';

    protected function grid(): Grid
    {
        $grid = new NGrid(new WithdrawModel);
        $grid->model()
            ->where('status', 1)->where('status_arrival', 1)->orderByDesc('withdraw.id')
            ->select([
                'withdraw.serial_number',
                'withdraw.payment_method_id',
                'withdraw.name',
                'withdraw.identity',
                'withdraw.bank_name',
                'withdraw.amount',
                'withdraw.file_id',
                'withdraw.remark',
                'withdraw.pay_at',
                'operate_id',
            ]);
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('serial_number', __('com.serial_number'));
                $filter->equal('withdraw-name', __('withdraw.name'));
                $filter->equal('bank_name', __('withdraw.bank_name'));
                $filter->where(function ($query) {
                    if ($this->input === 1) $query->whereNotNull('file_id');
                    else $query->whereNull('file_id');
                }, __('withdraw.file_id'))->select(__('com.whether'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('payment_method_id', __('withdraw.payment_method_id'))->select(__('withdraw.methodTypeArr'));
                $filter->equal('identity', __('withdraw.identity'));
                $filter->where(function ($query) {
                    $query->where('amount', '>=', $this->input);
                }, __('withdraw.amount') . '>=')->integer();
            });
        });
        $grid->column('serial_number', __('com.serial_number'))->style('width:300px');
        $grid->column('payment_method_id', __('withdraw.payment_method_id'))->using(__('withdraw.methodTypeArr'));
        $grid->column('name', __('withdraw.name'));
        $grid->column('identity', __('withdraw.identity'));
        $grid->column('bank_name', __('withdraw.bank_name'));
        $grid->column('amount', __('withdraw.amount'));
        if (is_null(request()->input('_export'))) {
            $grid->column('file_id', __('withdraw.file_id'))->display(function ($field) {
                return fileIdToUrl($field);
            })->image(config('oss.url'), 50);
        } else {
            $grid->column('file_id', __('withdraw.file_id'))->display(function ($field) {
                return config('oss.url') . fileIdToUrl($field);
            });
        }
        $grid->column('remark', __('withdraw.remark'));
        $grid->column('pay_at', __('withdraw.pay_at'));
//        $grid->column('admin_name', __('withdraw.admin_name'));
        $grid->disableCreateButton();
        $grid->exporter(new WithdrawExport($grid));
//        $grid->export(function ($export) {
//            $export->filename('Filename.csv');
////            $export->column('serial_number');
//            $export->only([
//                'serial_number',
//                'payment_method_id',
//                'name',
//                'identity',
//                'bank_name',
//                'amount',
//                'file_id',
//                'remark',
//                'pay_at',
//                'admin_name',
//            ])->setAttributes('width:100px');
//        });
        return $grid;
    }

    /**
     *
     * @param int $id
     * @return Show
     * @Author Sun Wu
     * @CreatedAt 2024/1/26 14:21
     *
     */
    protected function detail(int $id): Show
    {
        $show = new Show(WithdrawModel::query()->leftJoin('admin_dm', 'admin_dm.user_id', 'withdraw.operate_id')
            ->where('withdraw.id', $id)
            ->first(['withdraw.*', 'withdraw.file_id', 'admin_dm.name as admin_name']));
        $show->field('id', __('ID'));
        $show->field('serial_number', __('com.serial_number'));
        $show->field('payment_method_id', __('withdraw.payment_method_id'))->using(__('withdraw.methodTypeArr'));
        $show->field('name', __('withdraw.name'));
        $show->field('identity', __('withdraw.identity'));
        $show->field('bank_name', __('withdraw.bank_name'));
        $show->field('amount', __('withdraw.amount'));
        $show->field('file_id', __('withdraw.file_id'))->as(function ($field) {
            return fileIdToUrl($field);
        })->image(config('oss.url'), 100);
        $show->field('remark', __('withdraw.remark'));
        $show->field('pay_at', __('withdraw.pay_at'));
        $show->field('admin_name', __('withdraw.admin_name'));
        return $show;
    }
}
